package cn.lutu.domain.service.impl;

import cn.lutu.domain.repository.HotelRepository;
import cn.lutu.domain.repository.RoomRepository;
import cn.lutu.domain.service.HotelService;

import cn.lutu.protocol.dto.HotelAddDTO;
import cn.lutu.protocol.dto.HotelDTO;
import cn.lutu.protocol.entity.Hotel;
import cn.lutu.protocol.entity.Room;
import cn.lutu.protocol.vo.ArticleOthersListVO;
import cn.lutu.protocol.vo.HotelVO;
import cn.lutu.protocol.vo.RoomVO;
import cn.lutu.util.pojo.vo.PageData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
@Transactional
public class HotelServiceImpl implements HotelService {

    @Value("${lutu.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    HotelRepository hotelRepository;

    @Autowired
    RoomRepository roomRepository;

    @Transactional
    @Override
    public void insertHotel(HotelAddDTO hotelDTO) {
        Hotel hotel = new Hotel();
        BeanUtils.copyProperties(hotelDTO, hotel);
        hotel.setHotelStar("⭐⭐⭐⭐");
        hotel.setPhone(hotelDTO.getPhone());
        Room room=new Room();
        hotelRepository.insert(hotel);
        room.setRoomId(hotelDTO.getCategoryId());
        room.setPrice(hotelDTO.getPrice());
        room.setHotelId(hotel.getId());
        roomRepository.insert(room);
    }

    @Transactional
    @Override
    public void updateById(HotelDTO hotelDTO) {
        Hotel hotel = new Hotel();
        hotel.setId(hotelDTO.getHotelId())
                .setName(hotelDTO.getName())
                .setAddress(hotelDTO.getAddress())
                .setPhone(hotelDTO.getPhone());
        log.debug("hotel=" + hotel);
        hotelRepository.updateById(hotel);
        Room room=new Room();
        room.setId(hotelDTO.getId());
        room.setPrice(hotelDTO.getPrice());
        roomRepository.updateById(room);
    }

    @Override
    public void updateRoom(HotelDTO hotelDTO) {
        Hotel hotel = new Hotel();
        BeanUtils.copyProperties(hotelDTO, hotel);
        hotelRepository.updateRoom(hotel);
    }

    @Override
    public void deleteHotelById(Long id) {
        hotelRepository.deleteHotelById(id);
    }

    @Override
    public void deleteById(Long id) {
        hotelRepository.deleteById(id);
    }

    @Override
    public HotelVO selectHotel(Long id) {
        HotelVO hotelVO = hotelRepository.selectHotel(id);
        //查询功能
        return hotelVO;
    }

    @Override
    public HotelVO selectRoom(Long id) {
        HotelVO hotelVO = hotelRepository.selectRoom(id);
        return hotelVO;

    }

    @Override
    public PageData<HotelVO> selectHotelLIst(Integer pageNum, Integer pageSize) {
        log.debug("正在处理获取酒店列表业务");
        PageData<HotelVO> pageData = hotelRepository.selectHotelList(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<HotelVO> selectHotelLIst(Integer pageNum) {
        log.debug("开始处理【查询酒店列表】的业务，页码：{}", pageNum);
        PageData<HotelVO> pageData = hotelRepository.selectHotelList(pageNum, defaultQueryPageSize);
        return pageData;
    }


    @Override
    @ApiOperation("查询酒店详情")
    public HotelVO selectHotelLIstById(Long id) {
        log.debug("根据ID查询酒店数据，参数：{}",id);
        return hotelRepository.selectHotelListById(id);
    }

    @Override
    public List<RoomVO> selectRoomById(Long id) {
        log.debug("根据ID查询房间数据，参数{}",id);
        List<RoomVO> list = hotelRepository.selectRoomById(id);
        return list;
    }

    @Override
    public List<HotelVO> selectAllHotel() {
        return hotelRepository.selectAllHotel();
    }

    @Override
    public List<HotelVO> selectAllHotels() {
        return hotelRepository.selectAllHotels();
    }
}
